package com.manbearpig.todue;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class is responsible for interfacing with the DB
 * @author Brandon Devereaux
 * @version 1.0
 */
public class DatabaseHelper extends SQLiteOpenHelper
{
	private static final String DBNAME="todoDB.sqlite";
	
	/**
	 * Instantiate a DatabaseHelper
	 * @param context the context needed to pass to the super call
	 */
	public DatabaseHelper(Context context)
	{
		super(context,DBNAME,null,1);
	}
	
	/**
	 * This method is called when the db needs to be created
	 * @param db The database to perform queries on
	 */
	public void onCreate(SQLiteDatabase db)
	{
		db.beginTransaction();
		db.execSQL("CREATE TABLE Users(Username TEXT PRIMARY KEY, Name TEXT, Email TEXT, Password TEXT)");
		db.execSQL("CREATE TABLE Todos(_id INTEGER PRIMARY KEY AUTOINCREMENT, Username TEXT, Description TEXT, " +
				"Short_Description TEXT, Due_Date TEXT, Recurrence INTEGER, Category TEXT, Location TEXT, Completed TEXT)");
		Log.d("Checkpoint","db created");
		ContentValues cv = new ContentValues();
		cv.put("Username", "default");
		cv.put("Name", "default");
		cv.put("Email", "default@default.com");
		cv.put("Password", "default");
		db.insert("Users", null, cv);
		cv.clear();
		cv.put("Username", "default");
		cv.put("Description", "Test Item");
		cv.put("Short_Description", "short dsc");
		cv.put("Due_Date", "01/12/2012");
		cv.put("Recurrence", 0);
		cv.put("Category", "School");
		cv.put("Location", "location");
		cv.put("Completed","Not Completed");
		db.insert("Todos", null, cv);
		cv.clear();
		cv.put("Username", "default");
		cv.put("Description", "Test Item 2");
		cv.put("Short_Description", "short dsc");
		cv.put("Due_Date", "01/12/2012");
		cv.put("Recurrence", 0);
		cv.put("Category", "School");
		cv.put("Location", "location");
		cv.put("Completed", "Completed!");
		db.insert("Todos", null, cv);
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	/**
	 * This method is called when you need to change the schema of the database
	 * i.e. add or drop tables
	 * @param db The database to upgrade
	 * @param oldVersion the old database version number
	 * @param newVersion the new database version number
	 */
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		//Implement if dropping/ adding tables is needed in future	
	}
}
